﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class FinanceCenter_VirtualMoneyList : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            BindData();              //梆定所有数据
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        isAtFramePageLogin = true;                              //是框架页
        RequestLoginPage = "BalanceCenter/VirtualMoneyList.aspx";
        RequestCompetences = Competences.BuildCompetencesList(Competences.Administrator);

        base.OnInit(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.g.CurrentPageIndex = 0;
        this.gPager.PageIndex = 0;

        BindData();
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns[0].ColumnName = "序号";
            dt.Columns[1].ColumnName = "用户名";
            dt.Columns[2].ColumnName = "真实姓名";
            dt.Columns[3].ColumnName = "性别";
            dt.Columns[4].ColumnName = "身份证";
            dt.Columns[5].ColumnName = "奖金帐户余额";
            dt.Columns[6].ColumnName = "电子货币余额";
            dt.Columns[7].ColumnName = "返还货币余额";
      
            Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "VirtualMoneyList.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "VirtualMoneyList");
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        double Earning = 0;             // 奖金帐户
        double Earning2 = 0;            //总电子货币额
        double Earning3 = 0;            //总返还货币额

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Earning += Shove._Convert.StrToDouble(dt.Rows[i]["Earning"].ToString(), 0);
                Earning2 += Shove._Convert.StrToDouble(dt.Rows[i]["Earning2"].ToString(), 0);
                Earning3 += Shove._Convert.StrToDouble(dt.Rows[i]["Earning3"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + Earning + "</td>");
            output.Write("<td align='center'>" + Earning2 + "</td>");
            //output.Write("<td align='center'>" + Earning3 + "</td>");
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //DataRowView drv = (DataRowView)e.Item.DataItem;
            //DataRow dr = drv.Row;

            //double VirtualMoney = dr["VirtualMoney"].ToString();   
        }
    }

    /// <summary>
    /// 梆定条件查询出来的数据
    /// </summary>
    private void BindData()
    {
        dt = new DAL.Views.V_Customer().Open("ID,[CustomerNo],CustomerName,Sex,CertNo,Earning,Earning2,Earning3", "", "ID");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }
}
